﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class AccountDataAccess
    {
        #region 单例模式
        private AccountDataAccess()
        {
        }

        private static AccountDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static AccountDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new AccountDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        #region 登录相关
        /// <summary>
        /// 记录登录日志
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="logonType"></param>
        /// <param name="logonToken"></param>
        /// <param name="logonDeviceNo"></param>
        /// <param name="logonIp"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long AddLogonLog(long uid, short logonType, string logonToken, string logonDeviceNo, string logonIp,
            string nation, string province, string city, string district, out string stroutput)
        {
            stroutput = "记录登录日志失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_loginlog_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@logonType", DbType.Int16, logonType);
                db.AddInParameter(Procdbcomm, "@logonToken", DbType.String, logonToken);
                db.AddInParameter(Procdbcomm, "@logonDeviceNo", DbType.String, logonDeviceNo);
                db.AddInParameter(Procdbcomm, "@logonIp", DbType.String, logonIp);
                db.AddInParameter(Procdbcomm, "@nation", DbType.String, nation);
                db.AddInParameter(Procdbcomm, "@province", DbType.String, province);
                db.AddInParameter(Procdbcomm, "@city", DbType.String, city);
                db.AddInParameter(Procdbcomm, "@district", DbType.String, district);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("记录登录日志-AddLogonLog" + ex.ToString());
                return -99;
            }
        }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <returns></returns>
        public long RegisterAcc(string mobile, string memberName, string headerUrl, string openid, short logontype, int sex, out long uid, out short memberlevel, out string stroutput)
        {
            uid = 0;
            memberlevel = 0;
            stroutput = "注册账号失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_account_register";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@mobile", DbType.String, mobile);
                db.AddInParameter(Procdbcomm, "@membername", DbType.String, memberName);
                db.AddInParameter(Procdbcomm, "@headerurl", DbType.String, headerUrl);
                db.AddInParameter(Procdbcomm, "@logontype", DbType.Int16, logontype);
                db.AddInParameter(Procdbcomm, "@sex", DbType.Int32, sex);
                db.AddInParameter(Procdbcomm, "@openid", DbType.String, openid);
                db.AddOutParameter(Procdbcomm, "@uid", DbType.Int64, 18);
                db.AddOutParameter(Procdbcomm, "@memberlevel", DbType.Int16, 8);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                uid = (long)db.GetParameterValue(Procdbcomm, "@uid");
                memberlevel = (short)db.GetParameterValue(Procdbcomm, "@memberlevel");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("注册账号-RegisterAcc" + ex.ToString());
                return -99;
            }
        }

        /// <summary>
        /// 通过微信授权id查询账号信息
        /// </summary>
        /// <param name="openId">微信授权id</param>
        /// <returns></returns>
        public AccountInfo GetAccountByWx(int logonType, string openId)
        {
            AccountInfo acc = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select a.*,b.logon_type,b.open_key from ADZ_MemberMaster a left join ADZ_OpenReg b on a.uid=b.uid where open_key = @openid and logon_type = @logontype";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@openId", DbType.String, openId);
                db.AddInParameter(Procdbcomm, "@logontype", DbType.Int16, logonType);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    acc = new ModelHandler<AccountInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("通过微信授权id查询账号信息-GetAccountByWx" + ex.ToString());
            }
            return acc;

        }
        #endregion

        #region 账户相关
        /// <summary>
        /// 提现银行
        /// </summary>
        /// <returns></returns>
        public long EditCashBank(long uid, int bank_id, string bank_account, string branch_bank_name, string bank_account_name, out string stroutput)
        {
            long ret = 0;
            stroutput = "操作提现银行失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_cashbank_edit";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@bank_id", DbType.Int32, bank_id);
                db.AddInParameter(Procdbcomm, "@bank_account", DbType.String, bank_account);
                db.AddInParameter(Procdbcomm, "@branch_bank_name", DbType.String, branch_bank_name);
                db.AddInParameter(Procdbcomm, "@bank_account_name", DbType.String, bank_account_name);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("提现银行-EditCashBank" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 记录资金流水
        /// </summary>
        /// <returns></returns>
        public long AddAmountLog(long uid, decimal amt, int ordertype, string orderid, int inorout, string message, string remark, int is_show)
        {
            long ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_amountlog_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@amount", DbType.Decimal, amt);
                db.AddInParameter(Procdbcomm, "@ordertype", DbType.Int32, ordertype);
                db.AddInParameter(Procdbcomm, "@orderid", DbType.String, orderid);
                db.AddInParameter(Procdbcomm, "@inorout", DbType.Int32, inorout);
                db.AddInParameter(Procdbcomm, "@message", DbType.String, message);
                db.AddInParameter(Procdbcomm, "@remark", DbType.String, remark);
                db.AddInParameter(Procdbcomm, "@is_show", DbType.Int32, is_show);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("记录资金流水-AddAmountLog" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 根据手机号查询用户信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public AccountInfo GetAccount(string mobile)
        {
            AccountInfo acc = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = "select * from View_Member where account_mobile=@mobile and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@mobile", DbType.String, mobile);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    acc = new ModelHandler<AccountInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据手机号查询用户信息-GetAccount" + ex.ToString());
            }
            return acc;
        }

        /// <summary>
        /// 根据用户编号查询用户信息
        /// </summary>
        /// <param name="uid">用户编号</param>
        /// <returns></returns>
        public AccountInfo GetAccount(long uid)
        {
            AccountInfo acc = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = @"select * from View_Member where uid=@uid and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    acc = new ModelHandler<AccountInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据用户编号查询用户信息-GetAccount" + ex.ToString());
            }
            return acc;
        }

        /// <summary>
        /// 获取用户邀请码
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataSet GetInviteCode(long uid, int is_used, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_invitecode_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@is_used", DbType.Int32, is_used);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户邀请码-GetInviteCode" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 填写邀请码
        /// </summary>
        /// <param name="uid">被邀请人uid</param>
        /// <param name="code">邀请码</param>
        /// <returns></returns>
        public int WriteInviteCode(long rel_uid, string code, out long uid, out string mobile,
            out string rel_mobile, out string stroutput)
        {
            int ret = 0;
            uid = 0;
            rel_mobile = "";
            mobile = "";
            stroutput = "填写邀请码失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_invitecode_write";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@rel_uid", DbType.Int64, rel_uid);
                db.AddInParameter(Procdbcomm, "@code", DbType.String, code);
                db.AddOutParameter(Procdbcomm, "@invite_uid", DbType.Int64, 16);
                db.AddOutParameter(Procdbcomm, "@mobile", DbType.String, 100);
                db.AddOutParameter(Procdbcomm, "@rel_mobile", DbType.String, 100);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                uid = (long)db.GetParameterValue(Procdbcomm, "@invite_uid");
                rel_mobile = (string)db.GetParameterValue(Procdbcomm, "@rel_mobile");
                mobile = (string)db.GetParameterValue(Procdbcomm, "@mobile");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("填写邀请码-WriteInviteCode" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 会员升级申请
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="contact_name"></param>
        /// <param name="telephone"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long ApplyMemberUp(long uid, string contact_name, string telephone, out string stroutput)
        {
            long ret = 0;
            stroutput = "会员升级申请失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_memberup_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@contact_name", DbType.String, contact_name);
                db.AddInParameter(Procdbcomm, "@telephone", DbType.String, telephone);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("会员升级申请-ApplyMemberUp" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 申请买家
        /// </summary>
        /// <returns></returns>
        public long ApplySeller(long uid, string company_name, string contact_name, string telephone, string sex,
            string email, string qq, string company_address, out string stroutput)
        {
            long ret = 0;
            stroutput = "会员升级申请失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_seller_apply_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@company_name", DbType.String, company_name);
                db.AddInParameter(Procdbcomm, "@contact_name", DbType.String, contact_name);
                db.AddInParameter(Procdbcomm, "@telephone", DbType.String, telephone);
                db.AddInParameter(Procdbcomm, "@sex", DbType.String, sex);
                db.AddInParameter(Procdbcomm, "@email", DbType.String, email);
                db.AddInParameter(Procdbcomm, "@qq", DbType.String, qq);
                db.AddInParameter(Procdbcomm, "@company_address", DbType.String, company_address);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("申请买家-ApplySeller" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 资金流水记录
        /// </summary>
        /// <param name="orderType">1 充值 2 支付</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageRows"></param>
        /// <returns></returns>
        public DataSet GetAmountLogList(long uid, int orderType, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_amountlog_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@orderType", DbType.Int32, orderType);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户邀请码-GetInviteCode" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 设置支付密码
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int SetPayPwd(long uid, string paypwd, out string stroutput)
        {
            int ret = 0;
            stroutput = "设置支付密码失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_accout_paypwd";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@paypwd", DbType.String, paypwd);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("设置支付密码-SetPayPwd" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 填写发票
        /// </summary>
        /// <returns></returns>
        public long WriteInvoice(long id, long uid, int invoice_type, string invoice_title, string invoice_content, string taxpayer_ID, out string stroutput)
        {
            long ret = 0;
            stroutput = "填写发票失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_invoice_write";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, id);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@invoice_type", DbType.Int32, invoice_type);
                db.AddInParameter(Procdbcomm, "@invoice_title", DbType.String, invoice_title);
                db.AddInParameter(Procdbcomm, "@invoice_content", DbType.String, invoice_content);
                db.AddInParameter(Procdbcomm, "@taxpayer_ID", DbType.String, taxpayer_ID);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("填写发票-WriteInvoice" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取用户发票
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<AccountInvoice> GetInvoice(long uid)
        {
            List<AccountInvoice> info = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from ADZ_UserInvoice where uid=@uid";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                DataSet ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    info = new ModelHandler<AccountInvoice>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户发票-GetInvoice" + ex.ToString());
            }
            return info;
        }
        #endregion

        #region 收货地址
        /// <summary>
        /// 新增收货地址
        /// </summary>
        /// <param name="uid">用户userid</param>
        /// <param name="consignee">收货人</param>
        /// <param name="mobile">手机号</param>
        /// <param name="address">详细地址</param>
        /// <param name="isDefault">是否默认地址，一个用户只能默认一个地址</param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long AddAddress(long uid, string cityid, string cityname, string address, string receiver, string telephone, int isDefault, out string stroutput)
        {
            stroutput = "新增收货地址失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_address_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@cityid", DbType.String, cityid);
                db.AddInParameter(Procdbcomm, "@cityname", DbType.String, cityname);
                db.AddInParameter(Procdbcomm, "@address", DbType.String, address);
                db.AddInParameter(Procdbcomm, "@receiver", DbType.String, receiver);
                db.AddInParameter(Procdbcomm, "@telephone", DbType.String, telephone);
                db.AddInParameter(Procdbcomm, "@isdefault", DbType.Int16, isDefault);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("新增收货地址-AddAddress" + ex.ToString());
                return -99;
            }
        }


        /// <summary>
        /// 编辑收货地址
        /// </summary>
        /// <param name="id"></param>
        /// <param name="consignee">收货人</param>
        /// <param name="mobile">手机号</param>
        /// <param name="address">详细地址</param>
        /// <param name="isDefault">是否默认地址，一个用户只能默认一个地址</param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long EditAddress(long id, long uid, string cityid, string cityname, string address, string receiver, string telephone, int isDefault, out string stroutput)
        {
            stroutput = "编辑收货地址失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_address_edit";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@id", DbType.Int64, id);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@cityid", DbType.String, cityid);
                db.AddInParameter(Procdbcomm, "@cityname", DbType.String, cityname);
                db.AddInParameter(Procdbcomm, "@address", DbType.String, address);
                db.AddInParameter(Procdbcomm, "@receiver", DbType.String, receiver);
                db.AddInParameter(Procdbcomm, "@telephone", DbType.String, telephone);
                db.AddInParameter(Procdbcomm, "@isdefault", DbType.Int16, isDefault);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("编辑收货地址-EditAddress" + ex.ToString());
                return -99;
            }
        }

        /// <summary>
        /// 删除收货地址
        /// </summary>
        /// <returns></returns>

        public int DelAddress(long id, long uid, out long new_seq_id, out string stroutput)
        {
            int ret = 0;
            new_seq_id = 0;
            stroutput = "删除收货地址失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_address_del";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, id);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddOutParameter(Procdbcomm, "@new_seq_id", DbType.Int64, 16);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                new_seq_id = (long)db.GetParameterValue(Procdbcomm, "@new_seq_id");
                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("删除收货地址-DelAddress" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取用户收货地址列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataSet GetAddressList(long uid)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = "select * from ADZ_UserAddress where uid=@uid order by is_default desc,seq_id desc";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                return db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户收货地址列表-GetAddressList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 获取收货地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AddressInfo GetAddress(long id)
        {
            AddressInfo address = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = "select * from ADZ_UserAddress where seq_id=@id";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@id", DbType.Int64, id);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null)
                    address = new ModelHandler<AddressInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取收货地址-GetAddress" + ex.ToString());
            }
            return address;
        }
        #endregion

        #region 我的关注
        /// <summary>
        /// 处理我的关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="op_type">1 新增 2 取消</param>
        /// <param name="follow_type">1 分类 2 品牌</param>
        /// <param name="follow_id"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long HandleFollow(long uid, int op_type, int follow_type, long follow_id, out string stroutput)
        {
            long ret = 0;
            stroutput = "关注失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_memberfollow_handle";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@op_type", DbType.Int32, op_type);
                db.AddInParameter(Procdbcomm, "@follow_type", DbType.Int32, follow_type);
                db.AddInParameter(Procdbcomm, "@follow_id", DbType.Int64, follow_id);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("处理我的关注-HandleFollow" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 我的关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="follow_type"></param>
        /// <returns></returns>
        public DataTable GetFollow(long uid, int follow_type)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strStore = "proc_memberfollow_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(strStore);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@follow_type", DbType.Int32, follow_type);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("我的关注-GetFollow" + ex.ToString());
            }
            return dt;
        }
        #endregion
    }
}
